package com.gengine.resource
{
	import com.gengine.core.call.*;
	import com.gengine.debug.*;
	import com.gengine.resource.info.*;
	
	import flash.events.*;
	import flash.utils.*;
	
	public class LoaderManager extends Object
	{
		private var _completeCaller:Caller;
		private var _progressCaller:Caller;
		private var _errorCaller:Caller;
		public var maxQueue:int = 8;
		private var _queueMap:Dictionary;
		private var _currentCount:int = 0;
		private var _queueArray:Array;
		private var _highQueue:Array;
		
		public function LoaderManager()
		{
			if (_instance != null)
			{
				throw new Error("LoaderManager 单例  ");
			}
			
			this._queueArray = [];
			this._highQueue = [];
			this._completeCaller = new Caller();
			this._progressCaller = new Caller();
			this._errorCaller = new Caller();
		}
		
		public function load(param1:String, param2:Function, param3:int = 3, param4:Object = null, param5:Function = null, param6:Function = null) : void
		{
			var _loc_7:* = ResourceManager.getInfoByName(param1);
			if (ResourceManager.getInfoByName(param1) == null)
			{
				Log.error(param1 + "==没有该文件");
				if (param6 is Function)
				{
					param6(_loc_7);
				}
				
			}
			this.loadInfo(_loc_7, param2, param3, param4, param5, param6);
			
		}
		
		public function loadInfo(param1:ResourceInfo, param2:Function, param3:int = 3, param4:Object = null, param5:Function = null, param6:Function = null) : void
		{
			if (param4)
			{
				param1.extData = param4;
			}
			if (param1.isLoaded)
			{
				param2(param1);
				param1.addReference();
				
			}
			if (param1.isLoading)
			{
				this._completeCaller.addCall(param1.name, param2);
				if (param5 is Function)
				{
					this._progressCaller.addCall(param1.name, param5);
				}
				if (param6 is Function)
				{
					this._errorCaller.addCall(param1.name, param6);
				}
			}
			else
			{
				this._completeCaller.addCall(param1.name, param2);
				if (param5 is Function)
				{
					this._progressCaller.addCall(param1.name, param5);
				}
				if (param6 is Function)
				{
					this._errorCaller.addCall(param1.name, param6);
				}
				this.addLoader(param1, param3);
			}
			
		}
		
		private function addLoader(param1:ResourceInfo, param2:int = 3) : void
		{
			if (this.loadResource(param1) == false)
			{
				this.addQueue(param1, param2);
				param1.isLoading = true;
			}
			
		}
		
		private function loadResource(param1:ResourceInfo) : Boolean
		{
			
			return false;
		}
		
		private function addQueue(param1:ResourceInfo, param2:int = 0) : void
		{
			if (param2 >= 3)
			{
				this._queueArray.push(param1);
			}
			else if (param2 == 0)
			{
				this._highQueue.unshift(param1);
			}
			else if (param2 == 1)
			{
				this._highQueue.push(param1);
			}
			else if (param2 == 2)
			{
				this._queueArray.unshift(param1);
			}
			
		}
		
		private static var _instance:LoaderManager = new LoaderManager;
		public static function get instance() : LoaderManager
		{
			if (!_instance)
			{
				_instance = new LoaderManager;
			}
			return _instance;
		}
	}
}